var iconRenderer = function (row) {
  return '<li class="' + row.value + '"></li>'
};
$(function () {
  mini.parse();
  var roleGrid = mini.get("roleGrid");
  var table = mini.get("table");

  roleGrid.load("/system/list.json?sort=id&order=desc&offset=0&limit=10");

  window.selectionRole = function (grid) {
    var row = grid.selected;
    if (row) {
      table.load("/permission/list.json?sort=id&order=asc&offset=0&limit=10&id=" + row.id);
    }
  };

  window.SetData = function (result) {
    if (result.action === "edit") {
      var data = mini.clone(result);
    }
  };

  // 保存表单
  $('#save').on('click', function (ev) {
    var arr = [];
    var rows = table.getSelecteds();
    var json = url2json(window.location.search);
    if (rows.length > 0) {
      $.each(rows, function (i, item) {
        arr.push(item.id);
      });
      $.ajax({
        url: '/role/' + json.row_id + '/save_permissions',
        type: 'POST',
        contentType: "application/json",
        dataType: 'json',
        data: JSON.stringify(arr),
        success: function (result) {
          if (result.code === 200) {
            mini.alert('权限保存成功', '保存权限', function (action) {
              if (action === 'ok') {
                window.CloseOwnerWindow('ok');
              }
            });
          } else {
            mini.alert(result.msg);
          }
        }
      });
    }
  });
});